package cn.piesat.client.converter;

import cn.piesat.client.constant.enums.ClientRecordEnum;
import cn.piesat.client.model.dto.buiness.QsClientRunnerDto;
import cn.piesat.client.model.vo.QsTaskStatusVo;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class QsTaskStatusConverter {

    public QsTaskStatusVo task2RunStatusVo(QsClientRunnerDto runnerDto, String execServer){
        if(runnerDto==null){
            return null;
        }
        QsTaskStatusVo qsTaskStatusVo = new QsTaskStatusVo();
        qsTaskStatusVo.setTaskId(runnerDto.getTaskId());
        qsTaskStatusVo.setFileName(runnerDto.getFileName());
        qsTaskStatusVo.setExecJson(runnerDto.getExecJson());
        qsTaskStatusVo.setHasFlow(runnerDto.getHasFlow());
        qsTaskStatusVo.setAlgorithmId(runnerDto.getAlgorithmId());
        if(StringUtils.isNotBlank(execServer)){
            qsTaskStatusVo.setExecServer(execServer);
        }
        qsTaskStatusVo.setStartTime(String.valueOf(System.currentTimeMillis()));
        qsTaskStatusVo.setStatus(ClientRecordEnum.RUNNING.getRecordNumber());

        if(runnerDto.getOther()!=null){
            JSONObject jsonObject = JSONObject.parseObject(String.valueOf(runnerDto.getOther()));
            qsTaskStatusVo.setViewNumber(jsonObject.getString("viewNumber"));
        }
        return qsTaskStatusVo;
    }

    public QsTaskStatusVo task2EndStatusVo(QsTaskStatusVo taskStatusVo, ClientRecordEnum recordEnum){
        if(taskStatusVo==null){
            return null;
        }
        taskStatusVo.setStatus(recordEnum.getRecordNumber());
        taskStatusVo.setEndTime(String.valueOf(System.currentTimeMillis()));
        return taskStatusVo;
    }
}
